package com.buss.product.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.buss.core.dic.RecordStatus;
import com.buss.core.persistence.BaseDao;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.Paginalbe;
import com.buss.core.persistence.ResultList;
import com.buss.core.utils.StringUtil;
import com.buss.product.dao.GoodsCategoryDao;
import com.buss.product.model.GoodsCategory;

@Repository
public class GoodsCategoryDaoImpl implements GoodsCategoryDao {

	@Autowired
	private BaseDao<GoodsCategory> baseDao;
	
	@Override
	public GoodsCategory saveCategory(GoodsCategory category) {
		String sql = "update GoodsCategory category set category.subCategories = category.subCategories + 1 " +
				"where category.id = :parentId ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("parentId", category.getParentId());
		baseDao.excuteHsql(sql, params);
		return baseDao.saveOrEdit(category);
	}

	@Override
	public void editCategory(GoodsCategory category) {
		String sql = "update GoodsCategory category " +
				"set category.code = :code,category.name = :name," +
				"category.companyId=:companyId,category.branchId = :branchId," +
				"category.intro = :intro,category.parentId = :parentId," +
				"category.sortOrder = :sortOrder,category.subCategories = :subCategories," +
				"category.status = :status " +
				"where category.id = :id";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("code", category.getCode());
		params.put("name", category.getName());
		params.put("companyId", category.getCompanyId());
		params.put("branchId", category.getBranchId());
		params.put("intro", category.getIntro());
		params.put("parentId", category.getParentId());
		params.put("sortOrder", category.getSortOrder() == null ? 0 : category.getSortOrder());
		params.put("subCategories", category.getSubCategories() == null ? 0 : category.getSubCategories());
		params.put("status", category.getStatus());
		params.put("id", category.getId());
		baseDao.excuteHsql(sql, params);
	}

	@Override
	public void updateCategoryBrand(Long categoryId,Long[] brandIds){
		String sql = "delete from base_category_brand where categoryId = :categoryId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("categoryId", categoryId);
		baseDao.excuteNative(sql, params);
		if(brandIds == null || brandIds.length == 0){
			return;
		}
		sql = "insert into base_category_brand(categoryId,brandId) values(:categoryId,:brandId)";
		for(Long brandId : brandIds){
			params = new HashMap<String, Object>();
			params.put("categoryId", categoryId);
			params.put("brandId", brandId);
			baseDao.excuteNative(sql, params);
		}
	}
	
	public void updateCategoryPropGroup(Long categoryId,Long[] groupIds){
		String sql = "delete from base_category_propgroup where categoryId = :categoryId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("categoryId", categoryId);
		baseDao.excuteNative(sql, params);
		if(groupIds == null || groupIds.length == 0){
			return;
		}
		sql = "insert into base_category_propgroup(categoryId,groupId) values(:categoryId,:groupId)";
		for(Long groupId : groupIds){
			params = new HashMap<String, Object>();
			params.put("categoryId", categoryId);
			params.put("groupId", groupId);
			baseDao.excuteNative(sql, params);
		}
	}
	
	@Override
	public void removeCategory(Long[] ids) {
		String sql = "update GoodsCategory category set category.subCategories = category.subCategories - 1 " +
				"where category.id = :parentId ";
		for(Long id : ids){
			GoodsCategory category = loadCategory(id);
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("parentId", category.getParentId());
			baseDao.excuteHsql(sql, params);
			baseDao.remove(category);
		}
	}

	@Override
	public GoodsCategory loadCategory(Long id) {
		return baseDao.get(id, GoodsCategory.class);
	}

	@Override
	public GoodsCategory loadCategory(String companyId, String code) {
		String sql = "select category from GoodsCategory category " +
				"where category.companyId = :companyId and category.code = :code";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		params.put("code", code);
		return baseDao.loadHsql(sql, params);
	}

	@Override
	public ResultList<GoodsCategory> queryCategory(String companyId,
			String branchId,Long parentId,String name,  RecordStatus status,
			Long propGroupId, OrderBy orderBy, Paginalbe page) {
		String sql = "from GoodsCategory category where category.companyId = :companyId " +
				"and (category.branchId = '0' or category.branchId like :branchId) " +
				"and parentId = :parentId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		params.put("branchId", "," + branchId + ",");	
		params.put("parentId", parentId);
		if(StringUtil.isNotEmpty(name)){
			sql += " and category.name like :name ";
			params.put("name", "%" + name + "%");			
		}
		if(status != null){
			sql += " and category.status = :status ";
			params.put("status", status);
		}
		if(propGroupId != null){
			sql += " and category.propGroup.id = :propGroupId ";
			params.put("propGroupId", propGroupId);
		}
		if(orderBy == null){
			orderBy = new OrderBy();
			orderBy.add("sortOrder", OrderBy.DESC);
		}
		sql += orderBy.aggOrder("category");
		return baseDao.queryHsql(sql, params, page);
	}

}
